<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile;
use phpDocumentor\Reflection\Types\True_;

class UploadController extends Controller
{

    /**
     * 富文本编辑器上传图片
     *
     * @param Request $request
     * @return array
     */
    public function editorUpload(Request $request)
    {
        $data = $this->uploadImg($request);
        $url = $data['save_path'];
        return ['location' => $url];
    }

    /**
     * 上传单张图片
     *
     * @param Request $request
     * @return array
     */
    public function uploadImg(Request $request)
    {
        if (!$request->hasFile('file')){
            return [
                'code' => 1,
                'msg' => '非法请求'
            ];
        }
        $file = $request->file('file');
        // 校验图片
        $valid_res = $this->isValidImage($file);
        if ($valid_res['code'] !== 0){
            return $valid_res;
        }
        // 上传地址
        $path = 'upload/image/'.date('Y-m');
        // 若文件夹不存在就创建
        if (!file_exists($path)){
            $res = mkdir($path,0777,true);
            if (!$res){
                return [
                    'code' => 3,
                    'msg' => '文件夹创建失败'
                ];
            }
        }
        // 上传文件
        $file_name = $file->getClientOriginalName();
        $file->move($path,$file_name);
        if (file_exists($path.'/'.$file_name)){
            $new_path = '/'.$path.'/'.$file_name;
            return [
                'code' => 200,
                'state' => 'SUCCESS',
                'msg' => '上传成功',
                'save_path' => $new_path
            ];
        }else{
            return [
                'code' => 4,
                'msg' => '上传失败'
            ];
        }
    }


    /**
     * 校验图片
     *
     * @param UploadedFile $file
     * @return array|bool
     */
    public function isValidImage(UploadedFile $file)
    {
        if (!$file->isValid()) {
            return [
                'code'=> 5,
                'msg' => '文件无效'
            ];
        }
        if ($file->getSize() > config('admin_config.upload.uploadSize.imageMaxSize')){
            return [
                'code'=> 6,
                'msg' => '文件太大了'
            ];
        }
        if (!in_array('.'.strtolower($file->getClientOriginalExtension()),
            config('admin_config.upload.uploadSize.imageAllowFiles'))){
            return [
                'code'=> 7,
                'msg' => '文件格式不正确'
            ];
        }
        return [
            'code' => 0,
            'msg' => '校验成功'
        ];
    }

    public function uploadFile(Request $request)
    {
        if (!$request->hasFile('file')){
            return [
                'code' => 1,
                'msg' => '非法请求'
            ];
        }
        $file = $request->file('file');
        // 上传地址
        $path = 'upload/file/'.date('Y-m');
        // 若文件夹不存在就创建
        if (!file_exists($path)){
            $res = mkdir($path,0777,true);
            if (!$res){
                return [
                    'code' => 3,
                    'msg' => '文件夹创建失败'
                ];
            }
        }
        // 上传文件
        $file_name = $file->getClientOriginalName();
        $file->move($path,$file_name);
        if (file_exists($path.'/'.$file_name)){
            $new_path = '/'.$path.'/'.$file_name;
            return [
                'code' => 200,
                'state' => 'SUCCESS',
                'msg' => '上传成功',
                'save_path' => $new_path
            ];
        }else{
            return [
                'code' => 4,
                'msg' => '上传失败'
            ];
        }
    }
}
